package org.dtree.ow.common;

import ch.qos.logback.classic.LoggerContext;
import org.dtree.ow.models.User;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.integration.shiro.NutShiro;
import org.nutz.ioc.Ioc;
import org.nutz.lang.Encoding;
import org.nutz.lang.random.R;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;

/**
 * Created by dengwubo on 2017/9/19.
 */
public class SetUp implements org.nutz.mvc.Setup {
    public final Logger log = LoggerFactory.getLogger(Setup.class);
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

    @Override
    public void init(NutConfig config) {
        NutShiro.DefaultLoginURL= "/";
        NutShiro.DefaultNoAuthURL="/403.jsp";

        try {

            // 环境检查
            if (!Charset.defaultCharset().name().equalsIgnoreCase(Encoding.UTF8)) {
                log.warn("This project must run in UTF-8, pls add -Dfile.encoding=UTF-8 to JAVA_OPTS");
            }
            Ioc ioc = config.getIoc();

            Dao dao = ioc.get(Dao.class);
            // 初始化数据表
            initSysData(config, dao);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void initSysData(NutConfig config, Dao dao) {
        Daos.createTablesInPackage(dao,"org.dtree.ow",false);
        Daos.migration(dao,"org.dtree.ow",true,false,false);

        User surperMan = null;
        if (dao.count(User.class)==0) {
            surperMan = new User();
            surperMan.setId(R.UU32());
            surperMan.setPassword("123");
            surperMan.setAccount("zhangsan");
            surperMan = dao.insert(surperMan);
        }



    }


    @Override
    public void destroy(NutConfig nutConfig) {

    }

}


